package io.gitee.caoxiaoyu97.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.function.Function;

/*********************************************************
 * 文件名称：StreamUtil.java
 * 系统名称：cxycloud
 * 模块名称：io.gitee.caoxiaoyu97.messageconvert.util
 * 功能说明：流工具类
 * 开发人员 @author：caoxy31978
 * 开发时间 @date：2022/8/25 16:15
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class StreamUtil {
    public static byte[] readBytesAll(InputStream inputStream) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while (-1 != (len = inputStream.read(bytes))){
            output.write(bytes,0,len);
        }
        return output.toByteArray();
    }
    public static String readBytesAll(InputStream inputStream, Charset charset) throws IOException {
        return new String(readBytesAll(inputStream),charset);
    }
    public static String readString(InputStream inputStream, int length, Charset charset) {
        return new String(readBytes(inputStream, length).array(), charset);
    }
    public static String replaceXmlInfo(String xml, String startTag, String endTag, Function<String,String> function) {
        int start = xml.indexOf(startTag);
        int end = xml.indexOf(endTag,start);
        if (start == -1 || end == -1){
            return xml;
        }
        String oldValue = xml.substring(start + startTag.length(), end);
        String newValue = function.apply(oldValue);
        return xml.substring(0, start + startTag.length()) + newValue + xml.substring(end);
    }
    public static String getMsg(String xml, String startTag, String endTag){
        int start = xml.indexOf(startTag);
        int end = xml.indexOf(endTag,start);
        if (start == -1 || end == -1){
            return "";
        }
        return xml.substring(start + startTag.length(), end);
    }
    /**
     * Byte中1的和
     */
    public static int byteToBitSum(byte n) {
        int ret = 0;
        while (n != 0) {
            n &= n - 1;
            ret++;
        }
        return ret;
    }
    public static ByteBuffer readBytes(InputStream inputStream, int length) {
        ByteBuffer buffer;
        String errMsg;
        try {
            buffer = ByteBuffer.allocate(length);
            int capacity;
            int maxCapacity = 8192;
            capacity = length / maxCapacity == 0 ? length : maxCapacity;
            byte[] tmpBody = new byte[capacity];
            while (buffer.remaining() != 0 && (capacity = inputStream.read(tmpBody)) != -1) {
                buffer.put(tmpBody, 0, capacity);
                int remaining = buffer.remaining();
                capacity = remaining / maxCapacity == 0 ? remaining : maxCapacity;
                tmpBody = new byte[capacity];
            }
            if (buffer.remaining() != 0) {
                throw new RuntimeException(
                        "读取XX报文失败,已到达输入流的末尾[响应报文长度为" + length + ",已读取" + buffer.position() + "位]");
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            }
            errMsg = "读取XX报文失败";
            if (e instanceof SocketTimeoutException) {
                errMsg = "读取XX报文超时";
            }
            throw new RuntimeException(errMsg, e);
        }
        buffer.rewind();
        return buffer;
    }
}
